/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projekt;

/**
 * AI class that controls the movement of the monsters.
 */
public class Ai {

    /**
     * Stored variable for dungeon.
     */
    private Dungeon dg;

    /**
     * Constructor for the AI class takes a level and goes through its grid
     * performing AI actions for all monsters.
     *
     * @param dgx dungeon
     */
    Ai(final Dungeon dgx) {
        dg = dgx;

    }

    /**
     * AI method that loops through the level grid moving and attacking with
     * monsters if they are in range of the player.
     *
     * @param level1 level used.
     */
    public final void aiMonsters(final Level level1) {
        for (int x = 0; x < level1.getArrayLength(); x++) {
            for (int y = 0; y < level1.getArrayLength(); y++) {
                if (level1.getGrid(x, y) instanceof Monster) {
                    Monster tempMonster = (Monster) level1.getGrid(x, y);
                    if (level1.getGrid(x - 1, y) instanceof Hero) {
                        dg.getCombat().counterAttack((Creature) level1.getGrid(x, y),
                                (Hero) level1.getGrid(x - 1, y));
                    } else if (level1.getGrid(x + 1, y) instanceof Hero) {
                        dg.getCombat().counterAttack((Creature) level1.getGrid(x, y),
                                (Hero) level1.getGrid(x + 1, y));
                    } else if (level1.getGrid(x, y - 1) instanceof Hero) {
                        dg.getCombat().counterAttack((Creature) level1.getGrid(x, y),
                                (Hero) level1.getGrid(x, y - 1));
                    } else if (level1.getGrid(x, y + 1) instanceof Hero) {
                        dg.getCombat().counterAttack((Creature) level1.getGrid(x, y),
                                (Hero) level1.getGrid(x, y + 1));
                    } else if (level1.getGrid(x + 1, y + 1) instanceof Hero) {
                        if (level1.getGrid(x + 1, y).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x + 1, y, tempMonster);
                        } else if (level1.getGrid(x, y + 1).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x, y + 1, tempMonster);
                        }
                    } else if (level1.getGrid(x - 1, y + 1) instanceof Hero) {
                        if (level1.getGrid(x - 1, y).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x - 1, y, tempMonster);
                        } else if (level1.getGrid(x, y + 1).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x, y + 1, tempMonster);
                        }
                    } else if (level1.getGrid(x + 1, y - 1) instanceof Hero) {
                        if (level1.getGrid(x + 1, y).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x + 1, y, tempMonster);
                        } else if (level1.getGrid(x, y - 1).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x, y - 1, tempMonster);
                        }
                    } else if (level1.getGrid(x - 1, y - 1) instanceof Hero) {
                        if (level1.getGrid(x - 1, y).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x - 1, y, tempMonster);
                        } else if (level1.getGrid(x, y - 1).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x, y - 1, tempMonster);
                        }
                    }
                    if (level1.getGrid(x - 2, y) instanceof Hero) {
                        if (level1.getGrid(x - 1, y).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x - 1, y, tempMonster);
                        }
                    } else if (level1.getGrid(x + 2, y) instanceof Hero) {
                        if (level1.getGrid(x + 1, y).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x + 1, y, tempMonster);
                        }
                    } else if (level1.getGrid(x, y - 2) instanceof Hero) {
                        if (level1.getGrid(x, y - 1).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x, y - 1, tempMonster);
                        }
                    } else if (level1.getGrid(x, y + 2) instanceof Hero) {
                        if (level1.getGrid(x, y + 1).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x, y + 1, tempMonster);
                        }
                    } else if (level1.getGrid(x + 2, y + 2) instanceof Hero) {
                        if (level1.getGrid(x + 1, y).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x + 1, y, tempMonster);
                        } else if (level1.getGrid(x, y + 1).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x, y + 1, tempMonster);
                        }

                    } else if (level1.getGrid(x - 2, y + 2) instanceof Hero) {
                        if (level1.getGrid(x - 1, y).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x - 1, y, tempMonster);
                        } else if (level1.getGrid(x, y + 1).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x, y + 1, tempMonster);
                        }

                    } else if (level1.getGrid(x + 2, y - 2) instanceof Hero) {
                        if (level1.getGrid(x + 1, y).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x + 1, y, tempMonster);
                        } else if (level1.getGrid(x, y - 1).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x, y - 1, tempMonster);
                        }

                    } else if (level1.getGrid(x - 2, y - 2) instanceof Hero) {
                        if (level1.getGrid(x - 1, y).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x - 1, y, tempMonster);
                        } else if (level1.getGrid(x, y - 1).isIsWalkable()) {
                            level1.addLevelObject(x, y, new Floor());
                            level1.addLevelObject(x, y - 1, tempMonster);
                        }

                    }
                }
            }
        }
    }
}
